package cn.cdiothree.controller;


import cn.cdiothree.dataobject.MyPageHelper;
import cn.cdiothree.dataobject.ProductCategory;
import cn.cdiothree.dataobject.ProductInfo;
import cn.cdiothree.service.CarouselService;
import cn.cdiothree.service.ProductCategoryService;
import cn.cdiothree.service.ProductInfoService;
import cn.cdiothree.util.ResultVOUtil;
import cn.cdiothree.vo.ProductDetailVO;
import cn.cdiothree.vo.ProductInfoVO;
import cn.cdiothree.vo.ProductCategoryVO;
import cn.cdiothree.vo.ResultVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *买家商品
 * @author CumThing
 * @since 2020-02-02
 */
@RestController
@RequestMapping("/products")
public class ProductInfoController {

    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    ProductCategoryService productCategoryService;
    @Autowired
    CarouselService carouselService;


    /**
     * 查看商品列表
     * @return
     */
    @GetMapping("/list")
    public ResultVO list(){

        //查询商品,实现分页功能；
        IPage<ProductInfo> iPage=productInfoService.findAll(0,3);
        List<ProductInfo> productInfoList=iPage.getRecords();

        //查询类目

        List<Integer> parentIdList=productInfoList.stream()
                                    .map(e ->e.getParentId())
                                    .collect(Collectors.toList());
        List<ProductCategory> productCategoryList=productCategoryService.findByParentIdIn(parentIdList);

        //数据拼装
        List<ProductCategoryVO> productCategoryVOList =new ArrayList<>();
        for (ProductCategory productCategory:productCategoryList){
            ProductCategoryVO productCategoryVO =new ProductCategoryVO();
            productCategoryVO.setParentId(productCategory.getParentId());
            productCategoryVO.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVO> productInfoVOList=new ArrayList<>();
            for (ProductInfo productInfo:productInfoList){
                //判断类目是否相等
                if (productInfo.getParentId().equals(productCategory.getParentId())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    //从其它类拷贝方法
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productCategoryVO.setProductInfoVOList(productInfoVOList);
            productCategoryVOList.add(productCategoryVO);
        }

        return ResultVOUtil.success(productCategoryVOList);
    }

    /**
     * 查看商品列表
     * @param parentId
     * @return
     */
    @GetMapping("/list2")
    public ResultVO products(Integer parentId,Integer page) {
        MyPageHelper<ProductInfoVO> myPageHelper = productInfoService.getProductInfoVosByParentId(page, 2, parentId);
        return ResultVO.success(myPageHelper);
    }

    /**
     * 查看商品详情
     * @param Id
     * @return
     */
    @GetMapping("/product")
    public ProductDetailVO findById(Integer Id){
        ProductDetailVO productDetailVO =  productInfoService.findByProductId(Id);
        return productDetailVO;
    }
}
